﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Phong_CTSV
{
    public partial class AdminScholarShips : System.Web.UI.UserControl
    {
        public Form form;
        //University uni;
        List<Columns_Header> columnsHeader = new List<Columns_Header>();
        List<Columns_Header> allcolumns = new List<Columns_Header>();
        List<Rows_Header> rowsHeader = new List<Rows_Header>();
        public List<University> listUni = new List<University>();
        public Data_Table[] Sum;
        public string NameTable;
        Data_Table[,] dataTable = null;
        int year;
        int numColumn = 0;
        int numRow = 0;
        public bool scholarTable = false;
        public bool[] Hide;
        public int[] addColumHead;//= new int[] { 2,3,4,5,6,7,8};
        public int[] addIndexHead;//= new int[] { 0,1,2,3,4,5,6};
        QuerrySQL query = new QuerrySQL();
        public AdminScholarShips()
            : base()
        {
        }
        public int Year
        {
            get
            {
                return year;
            }
        }
        public List<Columns_Header> ColumnsHeaderList
        {
            get
            {
                return columnsHeader;
            }
        }
        public List<Rows_Header> RowsHeaderList
        {
            get
            {
                return rowsHeader;
            }
        }
        public Data_Table[,] DataTable
        {
            get
            {
                return dataTable;
            }
        }
        public int NumRow
        {
            get
            {
                return numRow;
            }
        }
        public int NumColumn
        {
            get
            {
                return numColumn;
            }
        }
        private void getNumRowAndColumn()
        {
            foreach (Columns_Header ch in columnsHeader)
            {
                if (!ch.Has_child)
                {
                    numColumn++;
                }
            }
            if (form.Has_rows)
            {
                foreach (Rows_Header rh in rowsHeader)
                {
                    if (!rh.Has_child)
                    {
                        numRow++;
                    }
                }

                // numColumn--;

            }
            else
            {
                if (form.IsSum)
                    numRow = 1;
                else
                {
                    int totalRow = 0;
                    for (int i = 1; i < listUni.Count; i++)
                    {
                        totalRow += query.maxRow(form.Name, listUni[i].Name, Year);
                    }
                    numRow = totalRow;
                }
            }
        }
        Data_Table[] getSum()
        {
            string total;
            Data_Table[] Sum = new Data_Table[NumColumn];
            for (int i = 0; i < NumColumn; i++)
            {
                Sum[i] = new Data_Table();
                Sum[i].Value_type = dataTable[0, i].Value_type;
                total = "";
                int numSum = 0;
                if (Sum[i].Value_type == "int" || Sum[i].Value_type == "average")
                    total = "0";
                for (int j = 0; j < listUni.Count - 1; j++)
                {
                    switch (dataTable[j, i].Value_type)
                    {
                        case "int":
                            total = (long.Parse(total) + long.Parse(dataTable[j, i].Value)).ToString();
                            break;
                        case "string":
                            total += dataTable[j, i].Value;
                            break;
                        case "average":
                            total = (long.Parse(total) + long.Parse(dataTable[j, i].Value)).ToString();
                            if (dataTable[j, i].Value != "0")
                                numSum++;
                            break;
                        default:
                            break;
                    }
                }
                if (Sum[i].Value_type == "average")
                    Sum[i].Value = (long.Parse(total) / (numSum)).ToString();
                else
                    Sum[i].Value = total;
            }
            return Sum;
        }
        Data_Table[,] getTable()
        {
            Data_Table[,] arrayData = new Data_Table[listUni.Count - 1, addIndexHead.Length];
            long total = 0;
            double totalAverage = 0;
            string totalString = "";
            int numR = numRow;
            int numSum = 0;
            if (form.IsSum)
            {
                for (int i = 1; i < listUni.Count; i++)
                {
                    for (int j = 0; j < addIndexHead.Length; j++)
                    {
                        arrayData[i - 1, j] = new Data_Table();
                        total = 0;
                        totalString = "";
                        totalAverage = 0;
                        numR = form.Has_rows ? numRow : query.maxRow(form.Name, listUni[i].Name, Year);
                        {
                            for (int k = 0; k < numR; k++)
                            {
                                Data_Table data = query.Select_Data_Cell(form.Id, listUni[i].ID, addIndexHead[j], k, Year);
                                switch (data.Value_type)
                                {
                                    case "int":
                                        total += long.Parse(data.Value);
                                        break;
                                    case "string":
                                        if (!string.IsNullOrEmpty(data.Value))
                                            totalString += (data.Value + Environment.NewLine);
                                        break;
                                    case "average":
                                        if (data.Value != "0")
                                        {
                                            totalAverage += double.Parse(data.Value);
                                            numSum++;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                arrayData[i - 1, j].Value_type = data.Value_type;
                            }
                        }
                        switch (arrayData[i - 1, j].Value_type)
                        {
                            case "int":
                                arrayData[i - 1, j].Value = total.ToString();
                                break;
                            case "string":
                                arrayData[i - 1, j].Value = totalString;
                                break;
                            case "average":
                                arrayData[i - 1, j].Value = (totalAverage / numSum).ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            else
            {
                arrayData = new Data_Table[NumRow, NumColumn];
                for (int k = 0; k < numR; k++)
                {
                    for (int j = 0; j < numColumn; j++)
                    {
                        numSum = 0;
                        arrayData[k, j] = new Data_Table();
                        total = 0;
                        totalString = "";
                        totalAverage = 0;
                        for (int i = 1; i < listUni.Count; i++)
                        {
                            Data_Table data = query.Select_Data_Cell(form.Id, listUni[i].ID, j, k, Year);
                            switch (data.Value_type)
                            {
                                case "int":
                                    total += long.Parse(data.Value);
                                    break;
                                case "string":
                                    if (!string.IsNullOrEmpty(data.Value))
                                        totalString += (data.Value + Environment.NewLine);
                                    break;
                                case "average":
                                    if (data.Value != "0")
                                    {
                                        totalAverage += double.Parse(data.Value);
                                        numSum++;
                                    }
                                    break;
                                default:
                                    break;
                            }
                            arrayData[k, j].Value_type = data.Value_type;
                        }
                        switch (arrayData[k, j].Value_type)
                        {
                            case "int":
                                arrayData[k, j].Value = total.ToString();
                                break;
                            case "string":
                                arrayData[k, j].Value = totalString;
                                break;
                            case "average":
                                arrayData[k, j].Value = (totalAverage / numSum).ToString();
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            return arrayData;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userName"] != null)
            {
                    year = System.DateTime.Now.Year;
                    string formName = "Bảng 5";
                    form = query.Select_Form(formName);
                    rowsHeader = query.Select_RowsHeaderByForm(formName);
                    allcolumns = query.Select_ColumnsHeaderByForm(formName);
                    for (int i = 0; i < addColumHead.Length; i++)
                    {
                        foreach (var item in allcolumns)
                        {
                            if (item.ID == addColumHead[i])
                            {
                                columnsHeader.Add(item);
                            }
                        }
                    }
                    listUni = query.Select_AllUniversity();
                    getNumRowAndColumn();
                    dataTable = getTable();
                    if (form.IsSum)
                        Sum = getSum();                
            }
            else
            {
                Response.Redirect("~/LoginPage.aspx");
            }
        }
    }
}
